//
//  FSLeaderDetailController.m
//  WZSOA
//
//  Created by 刘沿 on 16/7/4.
//  Copyright © 2016年 刘沿. All rights reserved.
//

#import "FSLeaderDetailController.h"
#import "FSLeaderDetailCell.h"


@interface FSLeaderDetailController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,strong) UIView *view1;

@property (nonatomic,strong) NSMutableArray *KindArray;

@property (nonatomic,strong) UITableView *tableView;

@property (nonatomic,strong) NSMutableArray *dataArray;

@property (nonatomic,strong) NSMutableArray *numberArray;

@property (nonatomic,strong) UIButton *lastbtn;

@property (nonatomic,assign) NSInteger index;

@end

@implementation FSLeaderDetailController

-(NSMutableArray *)numberArray{
    if (_numberArray == nil) {
        
        _numberArray = [NSMutableArray array];
    }

    return _numberArray;
}

-(NSMutableArray *)dataArray{
    if (_dataArray == nil) {
        
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}

-(NSMutableArray *)KindArray{

    if (_KindArray == nil) {
        
        _KindArray = [NSMutableArray array];
    }

    return _KindArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor colorWithRed:188/255.0 green:193/255.0 blue:196/255.0 alpha:1];
    
    UIView *view1 = [[UIView alloc] init];
    self.view1 = view1;
    view1.backgroundColor = [UIColor colorWithRed:236/255.0 green:247/255.0 blue:250/255.0 alpha:1];
    [self.view addSubview:view1];
    
    [view1 mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.view.mas_top).mas_offset(50);
        
        make.left.equalTo(self.view.mas_left).mas_offset(20);
        
        make.right.equalTo(self.view.mas_right).mas_offset(-20);
        
        make.height.equalTo(@100);
    }];
    
    //创建表格
    UITableView *tab = [[UITableView alloc] init];
    
    tab.delegate = self;
    
    tab.dataSource = self;
    
    self.tableView = tab;
    
    [self.view addSubview:tab];
    
    [tab mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(view1.mas_bottom);
        
        make.left.equalTo(self.view.mas_left).mas_offset(20);
        
        make.right.equalTo(self.view.mas_right).mas_offset(-20);
        
        make.bottom.equalTo(self.view.mas_bottom).mas_offset(-20);
    }];
//    [self.tableView addHeaderWithCallback:^{
//        [weakSelf setKind]; //加载网络数据
//        
//    }];
//    [self.tableView headerBeginRefreshing];
    
    [self setKind];
    
}


- (void)setKind{
    
    NSString *uesrId = [[NSUserDefaults standardUserDefaults] objectForKey:@"userId"];
    
    NSArray *array = @[@{@"field":self.field},@{@"userId":uesrId}];
    
    [HttpTools requestWithURL:KexchleaderURL arrays:array method:KgetLeader success:^(id JSON) {
        
        NSArray *tempArray = JSON[@"data"];
        
        NSMutableArray *dictArray1 = [NSMutableArray array];

        for (NSDictionary *dict in tempArray) {
            
            [dictArray1 addObject:dict[@"leader"]];
            
        }
        for (NSDictionary *dict in tempArray) {
            
            [dictArray1 addObject:dict[@"number"]];
            
        }
        self.KindArray = dictArray1;
        
        if (self.index == 0) {
            
            [self setUpKind:self.KindArray];

        }
        
        [self.tableView reloadData];
        [self.tableView headerEndRefreshing];
    } failure:^(NSError *error) {
        
    }];
    
}

//设置类型界面
- (void)setUpKind:(NSMutableArray *)arr{
    
    self.index ++;

    CGFloat ViewW = self.view.frame.size.width;
    
    CGFloat w = 0;//保存前一个button的宽以及前一个button距离屏幕边缘的距离
    CGFloat h = 10;//用来控制button距离父视图的高
    
    NSInteger count2 = arr.count * 0.5;
    
    NSArray *numberArray = [arr subarrayWithRange:NSMakeRange(count2,count2)];

    for (int i = 0; i < count2; i ++) {
        
        //创建按钮
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        
        NSString *str = numberArray[i];
        
        NSInteger tag = [str integerValue];
        
        btn.tag = tag;
        
        [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        
        [btn setTitleColor:[UIColor redColor] forState:UIControlStateSelected];
        
        [btn setTitle:arr[i] forState:UIControlStateNormal];
        
        [btn addTarget:self action:@selector(btnClick3:) forControlEvents:UIControlEventTouchUpInside];
        
        if (btn.tag == 0) {
            
            self.lastbtn = btn;
            
            [self loadMore:btn];

            btn.selected = YES;
        }

        //计算大小
        NSDictionary *attributes = @{NSFontAttributeName:[UIFont systemFontOfSize:15]};
        CGFloat length = [arr[i] boundingRectWithSize:CGSizeMake(ViewW, 200) options:NSStringDrawingUsesLineFragmentOrigin attributes:attributes context:nil].size.width;
        
        btn.frame = CGRectMake(10 + w, h, length + 40 , 30);
        //当button的位置超出屏幕边缘时换行 320 只是button所在父视图的宽度
        if(10 + w + length + 40 > ViewW){
            w = 0; //换行时将w置为0
            h = h + btn.frame.size.height + 10;//距离父视图也变化
            btn.frame = CGRectMake(10 + w, h, length + 40, 30);//重设button的frame
        }
        w = btn.frame.size.width + btn.frame.origin.x;
        
        
        [self.view1 addSubview:btn];
        
    }
    
    
}

- (void)btnClick3:(UIButton *)btn{
    btn.selected = YES;
    
    self.lastbtn.selected = NO;
    
    if (self.lastbtn == btn) {
        
        self.lastbtn.selected = YES;
        return;
    }
    
    [self.dataArray removeAllObjects];
    
    NSString *uesrId = [[NSUserDefaults standardUserDefaults] objectForKey:@"userId"];
    
    NSString *leader = btn.titleLabel.text;
    
    NSString *number = [NSString stringWithFormat:@"%zd",btn.tag];
    
    NSArray *array = @[@{@"field":self.field},@{@"leader":leader},@{@"number":number},@{@"userId":uesrId}];
    
    [HttpTools requestWithURL:Kexchleader arrays:array method:KcontentLeader success:^(id JSON) {
        
        NSArray *tempArray = JSON[@"data"];
        
        for (NSDictionary *dict in tempArray) {
            
            [self.dataArray addObject:dict[@"leader"]];
            
        }
        [self.tableView reloadData];
        [self.tableView headerEndRefreshing];
        
        self.lastbtn = btn;
    } failure:^(NSError *error) {
        
        
    }];
    
}

- (void)loadMore:(UIButton *)btn{

    NSString *uesrId = [[NSUserDefaults standardUserDefaults] objectForKey:@"userId"];
    
    NSString *leader = btn.titleLabel.text;
    
    NSString *number = [NSString stringWithFormat:@"%zd",btn.tag];
    
    NSArray *array = @[@{@"field":self.field},@{@"leader":leader},@{@"number":number},@{@"userId":uesrId}];
    
    [HttpTools requestWithURL:Kexchleader arrays:array method:KcontentLeader success:^(id JSON) {
        
        NSArray *tempArray = JSON[@"data"];
        
        for (NSDictionary *dict in tempArray) {
            
            [self.dataArray addObject:dict[@"leader"]];
            
        }
        
        self.lastbtn = btn;

        [self.tableView reloadData];
        [self.tableView headerEndRefreshing];
        
    } failure:^(NSError *error) {
        
        
    }];


}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return self.dataArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

   NSString *reuseID = @"detaiCell";
    
    FSLeaderDetailCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseID];
    
    if (cell == nil) {
        
        cell = [FSLeaderDetailCell LeaderDetailCell];
    }
    
    cell.array = self.dataArray;
    
    cell.indexPath = indexPath.row;
    
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    CGFloat H = 44;

    if (indexPath.row == 0) {
        
        H = [FSHightTool heightbyString:self.dataArray[0] font:15 minsWidth:60] + 50;
        
    }else if (indexPath.row == 1) {
        
        H = [FSHightTool heightbyString:self.dataArray[1] font:15 minsWidth:60]+ 50;
        
    }else if (indexPath.row == 2) {
        
        H = [FSHightTool heightbyString:self.dataArray[2] font:15 minsWidth:60]+ 50;
    }else if (indexPath.row == 3) {
        
        H = [FSHightTool heightbyString:self.dataArray[3] font:15 minsWidth:60]+ 50;
    }else if (indexPath.row == 4) {
        H = [FSHightTool heightbyString:self.dataArray[4] font:15 minsWidth:60]+ 50;
        
    }else if (indexPath.row == 5) {
        H = [FSHightTool heightbyString:self.dataArray[5] font:15 minsWidth:60]+ 50;
        
    }else {
    
    H = [FSHightTool heightbyString:self.dataArray[6] font:15 minsWidth:60]+ 50;
    
    }

    return H;
}





@end

































